﻿using System;
using System.Windows.Forms;

namespace RegistryGUI
{
    public partial class EditForms : Form
    {
        public string var;
        public string value;
        AddForm addForm = new AddForm();

        public EditForms()
        {
            InitializeComponent();
        }


        public void SetDataGridViewColumns(string regVar, string[] strs)
        {
            var = regVar;
            this.Text = var + " 环境变量";
            this.ColumnHeader.HeaderText = var + " 环境变量";
            dataGridView.Rows.Clear();
            foreach (string str in strs)
            {
                if (str != "") // 去除空行
                {
                    dataGridView.Rows.Add(str);
                }
            }
        }
        public string GetListViewItemToStr()
        {
            string str = "";
            foreach (DataGridViewRow row in this.dataGridView.Rows)
            {
                str += row.Cells[0].Value;
                str += ";";
            }
            return str;
        }
        public void SetSelectTextBox()
        {
            //textBox_Value.Focus();
            //textBox_Value.SelectAll();
            //this.ActiveControl = textBox_Value;//激活当前控件
        }
        private void Button_Dir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog选择目录 = new FolderBrowserDialog();
            if (folderBrowserDialog选择目录.ShowDialog() == DialogResult.OK && folderBrowserDialog选择目录.SelectedPath != string.Empty)
            {
                dataGridView.SelectedRows[0].Cells[0].Value = folderBrowserDialog选择目录.SelectedPath;//得到选中目录
            }
        }

        private void Button_File_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog选择文件 = new OpenFileDialog
            {
                Title = "打开"
            };
            if (openFileDialog选择文件.ShowDialog() == DialogResult.OK && openFileDialog选择文件.FileName != string.Empty)
            {
                //textBox_Value.Text = openFileDialog选择文件.FileName;//得到选中文件
            }
        }

        private void Button_OK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            value = GetListViewItemToStr();
            this.Hide();
        }

        private void Button_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            addForm.SetText(var, "");
            addForm.SetVarReadOnly();
            addForm.SetSelectValue();
            if (addForm.ShowDialog(this) == DialogResult.OK)
            {
                string newRegValue = addForm.Value;
                if (newRegValue != null || newRegValue != "")
                {
                    foreach (DataGridViewRow row in this.dataGridView.Rows)
                    {
                        if (row.Cells[0].Value.ToString() == newRegValue) // 判断是否重复
                        {
                            toolStripStatusLabel.Text = "新增失败，存在重复变量值";
                            return;
                        }
                    }
                    dataGridView.Rows.Add(newRegValue);
                    dataGridView.FirstDisplayedScrollingRowIndex = dataGridView.Rows.Count - 1; // 跳转最后一行
                    toolStripStatusLabel.Text = "新增成功，变量值为：" + newRegValue;
                }
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count >= 0)
            {
                dataGridView.CurrentCell = dataGridView.SelectedRows[0].Cells[0]; // 选中的单元格进入编缉状态
                dataGridView.BeginEdit(true);
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView.SelectedRows)  //选中项遍历
            {
                dataGridView.Rows.Remove(row);
            }
        }

        private void buttonUP_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection dgvsrc = dataGridView.SelectedRows;//获取选中行的集合
            if (dgvsrc.Count > 0)
            {
                int index = dataGridView.SelectedRows[0].Index;//获取当前选中行的索引
                if (index > 0)//如果该行不是第一行
                {
                    DataGridViewRow dgvr = dataGridView.Rows[index - dgvsrc.Count];//获取选中行的上一行
                    dataGridView.Rows.RemoveAt(index - dgvsrc.Count);//删除原选中行的上一行
                    dataGridView.Rows.Insert((index), dgvr);//将选中行的上一行插入到选中行的后面
                    for (int i = 0; i < dgvsrc.Count; i++)//选中移动后的行
                    {
                        dataGridView.Rows[index - i - 1].Selected = true;
                    }
                }
            }
        }

        private void buttonDown_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection dgvsrc = dataGridView.SelectedRows;//获取选中行的集合
            if (dgvsrc.Count > 0)
            {
                int index = dataGridView.SelectedRows[0].Index;//获取当前选中行的索引
                if (index >= 0 & (dataGridView.RowCount - 1) != index)//如果该行不是最后一行
                {
                    DataGridViewRow dgvr = dataGridView.Rows[index + 1];//获取选中行的下一行
                    dataGridView.Rows.RemoveAt(index + 1);//删除原选中行的上一行
                    dataGridView.Rows.Insert((index + 1 - dgvsrc.Count), dgvr);//将选中行的上一行插入到选中行的后面
                    for (int i = 0; i < dgvsrc.Count; i++)//选中移动后的行
                    {
                        dataGridView.Rows[index + 1 - i].Selected = true;
                    }
                }
            }
        }

        private void buttonText_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            editForm.SetVarReadOnly();
            string oldRegVar = "path";
            string oldRegValue = GetListViewItemToStr();

            editForm.SetText(oldRegVar, oldRegValue);
            editForm.SetSelectTextBox();

            // 编缉时考虑是否重复
            if (editForm.ShowDialog(this) == DialogResult.OK)
            {
                string newRegVar = editForm.var;
                string newRegValue = editForm.value;
                string[] strs = newRegValue.Split(';'); // 转换为以;分割的字符串数组
                this.SetDataGridViewColumns(newRegVar, strs);
            }
            return;
        }
    }
}
